package com.systemic.service.impl;

import com.systemic.mapper.EmotionLogMapper;
import com.systemic.pojo.EmotionLog;
import com.systemic.service.EmotionLogService;
import com.systemic.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: EmotionLogServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-01-22 17:34
 * @Version:
 **/
@Service
public class EmotionLogServiceImpl implements EmotionLogService {

    @Resource
    private EmotionLogMapper emotionLogMapper;


    /**
     * 新增
     *
     * @param emotionLog
     * @return
     */
    @Override
    @Transactional
    public int add(EmotionLog emotionLog) {
        Date date = new Date();
        emotionLog.setGmtCreate(date);
        emotionLog.setGmtModified(date);
        int row = emotionLogMapper.insert(emotionLog);
        if (row==0){
            throw new RuntimeException("新增失败");
        }
        return row;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public int deleteList(Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        return emotionLogMapper.deleteBatchIds(idList);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int delete(Integer id) {
        EmotionLog emotionLog = new EmotionLog();
        emotionLog.setId(id);
        return emotionLogMapper.delete(emotionLog);
    }

    /**
     * 修改
     * @param emotionLog
     * @return
     */
    @Override
    @Transactional
    public int update(EmotionLog emotionLog) {
        Date date = new Date();
        emotionLog.setGmtCreate(date);
        emotionLog.setGmtModified(date);
        int row = emotionLogMapper.update(emotionLog);
        if (row==0){
            throw new RuntimeException("修改失败");
        }
        return row;
    }

    /**
     * 分页查询
     *
     * @param pageVo
     * @return
     */
    @Override
    public List<EmotionLog> selectList(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        // 先拿到用户id
        Integer uid = pageVo.getUid();
        return emotionLogMapper.selectListByUidPage(uid,offset,pageSize);
    }

    /**
     * 指定查询
     *
     * @param id
     * @return
     */
    @Override
    public EmotionLog selectById(Integer id) {
        EmotionLog emotionLog = new EmotionLog();
        emotionLog.setId(id);
        return emotionLogMapper.selectFromId(emotionLog);
    }
}
